package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.School;
import com.yunji.news.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 院校控制器
 *
 * @author yunji
 * @since 2024-11-23
 */
@RestController
@RequestMapping("/schools")
@CrossOrigin
public class SchoolController {
    
    @Autowired
    private SchoolService schoolService;
    
    /**
     * 分页查询院校列表
     */
    @GetMapping
    public Result<IPage<School>> getSchools(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String schoolType) {
        
        IPage<School> result = schoolService.getSchoolPage(page, size, keyword, city, schoolType);
        return Result.success(result);
    }
    
    /**
     * 获取院校Logo列表（用于首页展示）
     */
    @GetMapping("/logos")
    public Result<List<School>> getSchoolLogos() {
        List<School> schools = schoolService.getSchoolLogos();
        return Result.success(schools);
    }
    
    /**
     * 获取院校详情
     */
    @GetMapping("/{id}")
    public Result<School> getSchool(@PathVariable Long id) {
        School school = schoolService.getById(id);
        if (school == null) {
            return Result.error("院校不存在");
        }
        return Result.success(school);
    }
    
    /**
     * 获取院校排名列表
     */
    @GetMapping("/ranking")
    public Result<List<School>> getSchoolRanking(
            @RequestParam(defaultValue = "50") Integer limit) {
        List<School> schools = schoolService.getSchoolRanking(limit);
        return Result.success(schools);
    }
    
    /**
     * 获取院校联系电话列表
     */
    @GetMapping("/phones")
    public Result<List<School>> getSchoolPhones() {
        List<School> schools = schoolService.getSchoolPhones();
        return Result.success(schools);
    }
}